package com.tx.axure.tkaxureweb.servlet;

import com.tx.axure.tkaxureweb.constant.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class FileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        String requestUrl=request.getRequestURI();
        String fileName=requestUrl.substring(requestUrl.indexOf("axure")+6);
        if(null==request.getSession().getAttribute(Constant.LOGIN_SESSION_KEY)){
            response.sendRedirect("/login");
        }else{
            response.setHeader("X-Accel-Redirect","/ax/" + fileName);
        }

//        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//        response.setHeader("Content-Type", "text/html");
//        response.setHeader("X-Accel-Redirect","/ax/" + fileName);
//        response.setHeader("X-Accel-Limit-Rate","1024");//限速，单位字节，默认不限
//        response.setHeader("X-Accel-Buffering","yes");//是否使用Nginx缓存，默认yes
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doGet(req, resp);
    }

}
